<?php

use Biz\cache\wiki\WikiBasicCounter;
use Biz\model\GoodsTestActivity\FineGoodsContentModel;
use Biz\service\wiki\Wiki as sWiki;
use Biz\model\wiki\Wiki as mWiki;
use Biz\service\wiki\WikiContent;
use Doraemon\model\AdmUserType;
use Doraemon\service\es\EsObjRelateWikiSupplement;
use Doraemon\model\Elasticsearch\EsObjRelateWikiModel;

/**
 * wiki详情页下面的  住友说、品牌说、门店说、测评报告、全部 计数器
 */
class getWikiCommentCounterAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;


    public function main()
    {
        $wiki_id = $this->getRequest()->get('wiki_id', ''); //开发环境 测试数据 wiki_id:1
        $spuInfo = sWiki::getWikiBaseInfos([$wiki_id]);
        $spuInfo = ($spuInfo && isset($spuInfo[$wiki_id])) ? $spuInfo[$wiki_id] : [];
        $basicId = $spuInfo ? $spuInfo['basic_id'] : 0;
        $data = WikiBasicCounter::getOne($basicId);
        $testObjIds = [];
        if ($basicId) {
            $testObjInfo = FineGoodsContentModel::all([
                'basic_id' => $basicId,
                'is_delete[!]' => FineGoodsContentModel::IS_DELETE_TRUE,
                'ORDER' => ['points' => 'DESC', 'id' => 'DESC'],
            ], '*');
            $uids = array_column($testObjInfo,'uid');
            $testObjIds = array_column($testObjInfo,'obj_id');
            $black_uids = AdmUserType::isBlevelMulti($uids);//B级
            $filter_uids = array_merge($black_uids, WikiContent::FILTER_UIDS);
            $wikiIds = mWiki::selectByWhere([
                    'basic_id' => $basicId,
                    'status[!]' => mWiki::STATUS_DEL,
                ], 'id');
            if($testObjIds){
//                $result = EsObjRelateWikiSupplement::search([
//                    'wiki_id_arr' => $wikiIds,
//                    'obj_id_arr' => $testObjIds,
//                    'filter_uid_arr' => $filter_uids,
//                    'pagesize' => 20,
//                ]);
                $result = EsObjRelateWikiModel::search([
                    'wiki_id_arr' => $wikiIds,
                    'obj_id_arr' => $testObjIds,
                    'filter_uid_arr' => $filter_uids,
                ],1,20);
                $testObjIds = array_unique(array_column($result['rows'], 'obj_id'));
            }
        }
        $res['basic_user_comment_num'] = $data[WikiBasicCounter::BASIC_USER_COMMENT_NUM] ?? 0;
        $res['basic_brand_comment_num'] = $data[WikiBasicCounter::BASIC_BRAND_COMMENT_NUM] ?? 0;
        $res['basic_store_comment_num'] = $data[WikiBasicCounter::BASIC_STORE_COMMENT_NUM] ?? 0;
        $res['basic_test_report_comment_num'] = count($testObjIds) ?? 0;
        $res['basic_all_comment_num'] = array_sum($res);

        $this->setSuccess($res);
    }

}
